package park.job;

import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.util.HashMap;

import jxl.Workbook;
import jxl.read.biff.BiffException;
import jxl.write.Label;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;
import jxl.write.biff.RowsExceededException;

public class ExcelOpr {
	public static String filePath = System.getProperty("user.dir")+File.separator+"log";

	private static WritableWorkbook workbook;
	private static Workbook wb;
	private static WritableSheet sheet;
	private static File file;

	public ExcelOpr(String fileName) {
		try {
			file = new File(filePath+File.separator+fileName+".xls");
			if (!file.exists()) {

				file.createNewFile();
				workbook = Workbook.createWorkbook(file);
				sheet = workbook.createSheet("First Sheet", 0);
				addCell(0,"interfaceName","execute time","cost time(ms)");
			} else {
				wb = Workbook.getWorkbook(file);

				workbook = Workbook.createWorkbook(file, wb); // 创建一个可读写的副本
				sheet = workbook.getSheet("First Sheet");

			}
			//
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (BiffException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (RowsExceededException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (WriteException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	



	public static WritableWorkbook getWorkbook() {
		return workbook;
	}

	public static WritableSheet getSheet() {
		return sheet;
	}

	public static void createExcel() throws WriteException, IOException {
		// 创建工作薄
		// WritableWorkbook workbook = Workbook.createWorkbook(os);
		// WritableWorkbook workbook = Workbook.createWorkbook(new
		// File(fileName));
		// 创建新的一页
		// WritableSheet sheet = workbook.createSheet("First Sheet", 0);
		// 创建要显示的内容,创建一个单元格，第一个参数为列坐标，第二个参数为行坐标，第三个参数为内容
		addCell(1, "11", "22", "44");
		// 把创建的内容写入到输出流中，并关闭输出流
		workbook.write();

	}

	public static void main(String[] args) throws WriteException, IOException {
		System.out.println(System.getProperty("user.dir"));//user.dir指定了当前的路径 
		file = new File(filePath+File.separator+"aa.xls");
		if (!file.exists()) {

			file.createNewFile();}
		
		
	}

	public static void addCell(int i, String funcName, String costTime, String executeTime)
			throws RowsExceededException, WriteException {

		Label lable0 = new Label(0, i, funcName);
		sheet.addCell(lable0);
		Label lable1 = new Label(1, i, costTime);
		sheet.addCell(lable1);
		Label label2 = new Label(2, i, executeTime);
		sheet.addCell(label2);

	}

	public static void writeAndCancle() {
		try {
			workbook.write();
			workbook.close();

		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (WriteException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public static void cancelBook() {
		try {
			workbook.close();
		} catch (WriteException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
